package com.ajl.wx.module.admin.controller;


import com.ajl.wx.config.WebApplicationProperties;
import com.ajl.wx.beans.po.CoursePO;
import com.ajl.wx.module.course.service.impl.TbCourseServiceImpl;
import com.ajl.wx.module.oss.service.IOssService;
import com.ajl.wx.beans.vo.ResultApi;
import com.ajl.wx.utils.FileUtil;
import com.ajl.wx.utils.ResultApiUtil;
import com.github.pagehelper.PageHelper;
import net.coobird.thumbnailator.Thumbnails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * <p>
 * </p>
 *
 * @author HouXinLin
 * @since 2020-09-17
 */
@Controller
@RequestMapping("admin/api/course/")
public class AdminCourseController {
    private Logger logger = LoggerFactory.getLogger(AdminCourseController.class);
    @Autowired
    TbCourseServiceImpl courseService;

    @Autowired
    IOssService iOssService;
    @Autowired
    WebApplicationProperties webApplicationProperties;

    @GetMapping("listCourse")
    public String listCourse(Model model, @RequestParam(value = "page", required = false, defaultValue = "1") int page) {
        PageHelper.startPage(page, webApplicationProperties.getPagehelperPageSize());
        model.addAttribute("courseSet", courseService.listCourse());
        return "admin/admin-list-video";
    }

    @PostMapping("courseUpload")
    @ResponseBody
    public ResultApi courseUpload(CoursePO coursePO,
                                  @RequestParam MultipartFile mCover,
                                  @RequestParam MultipartFile mVideo) {


        /**
         * 生成封面文件名
         */
        String videoCoverImgFileName = FileUtil.generateFileMd5(mCover.getOriginalFilename()) +
                FileUtil.getFileExtendNameForContentType(true, mCover);

        /**
         * 生成视频文件名称
         */
        String videoFileName = FileUtil.generateFileMd5(mVideo.getOriginalFilename()) +
                FileUtil.getFileExtendNameForContentType(true, mVideo);

        File fileImage = FileUtil.generateFile(webApplicationProperties.getFileImagePath(), videoCoverImgFileName);

        try {
            Thumbnails.of(mCover.getInputStream()).scale(1).outputQuality(.5).toFile(fileImage);
            iOssService.saveFile("images/"+videoCoverImgFileName,fileImage);
            iOssService.saveFile("video/"+videoFileName,mVideo.getInputStream());

            coursePO.setCourseReleaseTimestamp(LocalDateTime.now());
            //保存
            coursePO.setVideoUrl(videoFileName);
            coursePO.setVideoCover(videoCoverImgFileName);

            logger.info("上传视频,文件名称{},大小{}", mVideo.getName(), mVideo.getSize() / 1024);
            int save = courseService.insert(coursePO);
            return ResultApiUtil.success("OK", save);
        } catch (IOException e) {
            logger.error(e.getMessage());

        } finally {
        }
        return ResultApiUtil.fail(-1, "上传失败", null);
    }
}
